<?php
namespace app\api\controller\wdsxh;
use addons\wdsxh\library\Wxapp;
use app\admin\model\wdsxh\Config;
use app\admin\model\wdsxh\Member;
use think\Exception;
use Overtrue\Pinyin\Pinyin;

/**
 * 电子会牌
 */
class Willbrand extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index(){
        $member=Member::where('uid',$this->auth->id)->find();
        if(!$member || $member->status != '1'){
            $this->error('会员信息不存在或会员状态错误');
        }
        if(strtotime($member->expiretime) < time()) {
            $this->error('会员信息已过期,请重新缴纳会费');
        }
        $member->level;
        $res=$this->create($member->toArray());
        if($res && is_array($res)){
            $this->success(array('url'=>$res['url']));
        }else{
            $this->error('创建电子会牌失败');
        }
    }
    private function create($member){
        try{
            $qrcode=Wxapp::getQrcode('/pages/Component/Module/Main/Program/member/index/index?id='.$member['id']);
            if(empty($qrcode)){
                throw new Exception('创建二维码失败');
            }
            $data = [
                'bg'=>[
                    'img'=>(new Config())->where('id',1)->value('meeting_cards'),
                    'width'=>448,
                ],
                'data'=>[
                    [
                        'left'=>'160px',
                        'top' => '337px',
                        'type' => 'text',
                        'item' => 'post',
                        'lab' => '职务',
                        'width' => '101px',
                        'height' => '35px',
                        'size' => '25px',
                        'color'=>'#000000'
                    ],
                    [
                        'left'=>'209px',
                        'top' => '108px',
                        'type' => 'text',
                        'item' => 'cardid',
                        'lab' => '卡号',
                        'width' => '78px',
                        'height' => '23px',
                        'size' => '10px',
                        'color'=>'#000000',
                    ],
                    [
                        'left'=>'45px',
                        'top' => '669px',
                        'type' => 'img',
                        'item' => 'qr',
                        'lab' => '二维码',
                        'width' => '108px',
                        'height' => '108px',
                    ],
                    [
                        'left'=>'167px',
                        'top' => '255px',
                        'type' => 'text',
                        'item' => 'name',
                        'lab' => '姓名',
                        'width' => '78px',
                        'height' => '23px',
                        'size' => '14px',
                        'color'=>'#000000',
                    ],
                    [
                        'left'=>'125px',
                        'top' => '294px',
                        'type' => 'text',
                        'item' => 'period',
                        'lab' => '日期',
                        'width' => '78px',
                        'height' => '22px',
                        'size' => '12px',
                        'color'=>'#000000',
                    ],
                    [
                        'left'=>'181px',
                        'top' => '531px',
                        'type' => 'text',
                        'item' => 'expire',
                        'lab' => '过期日期',
                        'width' => '106px',
                        'height' => '22px',
                        'size' => '14px',
                        'color'=>'#000000',
                    ],
                ]
            ];
            $bgImg=ROOT_PATH.'public'.$data['bg']['img'];
            if(!file_exists($bgImg)){
                throw new Exception('海报背景图片不存在');
            }
            $mainLayer = \PHPImageWorkshop\ImageWorkshop::initFromPath($bgImg);
            $mainLayer->resizeInPixel($data['bg']['width'], null, true);
            $Layer=array();
            $fontPath = ROOT_PATH.'public'.DS.'assets'.DS.'addons'.DS.'wdsxh'.DS.'fonts'.DS.'AlibabaSans-BoldItalic.otf';
            $introduce= \app\admin\model\wdsxh\Introduce::where('id',1)->find();
            $pinyin = new Pinyin();
            $post=$member['level']['name'];
            $name=$member['name'];
            $cardid=$pinyin->abbr($introduce['name']).'-'.$pinyin->abbr($post).'-'.$pinyin->abbr($name);

            $period=date('Y年m月d日',strtotime($member['jointime']));
            $expire=date('Y年m月d日',strtotime($member['expiretime']));
            foreach ($data['data'] as $k=>$row){
                if(in_array($row['type'],array('img','text'))){
                    switch ($row['item']){
                        case 'avatar':
                            if(empty($avatar)){
                                break;
                            }
                            if($row['model']){
                                $Layer[$k]=\PHPImageWorkshop\ImageWorkshop::initFromPath($avatar);
                            }else{
                                $Layer[$k]=\PHPImageWorkshop\ImageWorkshop::initFromResourceVar(self::radius_img($avatar));
                            }
                            $Layer[$k]->resizeInPixel(intval($row['width']), intval($row['height']), false);
                            $mainLayer->addLayerOnTop($Layer[$k], intval($row['left']), intval($row['top']), 'LT');
                            break;
                        case 'qr':
                            if(empty($qrcode)){
                                break;
                            }
                            $Layer[$k]=\PHPImageWorkshop\ImageWorkshop::initFromPath($qrcode);
                            $Layer[$k]->resizeInPixel(intval($row['width']), intval($row['height']), false);
                            $mainLayer->addLayerOnTop($Layer[$k],  intval($row['left']),intval($row['top']), 'LT');
                            break;
                        case 'post':
                            if(empty($post)){
                                break;
                            }
                            $text = $post;
                            $fontSize = intval($row['size']);
                            $fontColor = substr($row['color'],1);
                            $textRotation = 0;
                            $backgroundColor = null;
                            $Layer[$k] = \PHPImageWorkshop\ImageWorkshop::initTextLayer($text, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
                            $mainLayer->addLayerOnTop($Layer[$k],intval($row['left']), intval($row['top'])+6,'LT');
                            break;
                        case 'cardid':
                            if(empty($cardid)){
                                break;
                            }
                            $text = $cardid;
                            $fontSize = intval($row['size']);
                            $fontColor = substr($row['color'],1);
                            $textRotation = 0;
                            $backgroundColor = null;
                            $Layer[$k] = \PHPImageWorkshop\ImageWorkshop::initTextLayer($text, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
                            $mainLayer->addLayerOnTop($Layer[$k],intval($row['left']), intval($row['top'])+6,'LT');
                            break;
                        case 'name':
                            if(empty($name)){
                                break;
                            }
                            $text = $name;
                            $fontSize = intval($row['size']);
                            $fontColor = substr($row['color'],1);
                            $textRotation = 0;
                            $backgroundColor = null;
                            $Layer[$k] = \PHPImageWorkshop\ImageWorkshop::initTextLayer($text, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
                            $mainLayer->addLayerOnTop($Layer[$k],intval($row['left']), intval($row['top'])+6,'LT');
                            break;
                        case 'period':
                            if(empty($period)){
                                break;
                            }
                            $text = $period;
                            $fontSize = intval($row['size']);
                            $fontColor = substr($row['color'],1);
                            $textRotation = 0;
                            $backgroundColor = null;
                            $Layer[$k] = \PHPImageWorkshop\ImageWorkshop::initTextLayer($text, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
                            $mainLayer->addLayerOnTop($Layer[$k],intval($row['left']), intval($row['top'])+6,'LT');
                            break;
                        case 'expire':
                            if(empty($expire)){
                                break;
                            }
                            $text = $expire;
                            $fontSize = intval($row['size']);
                            $fontColor = substr($row['color'],1);
                            $textRotation = 0;
                            $backgroundColor = null;
                            $Layer[$k] = \PHPImageWorkshop\ImageWorkshop::initTextLayer($text, $fontPath, $fontSize, $fontColor, $textRotation, $backgroundColor);
                            $mainLayer->addLayerOnTop($Layer[$k],intval($row['left']), intval($row['top'])+6,'LT');
                            break;
                    }
                }
            }
            $dirPath = empty($savepath)?ROOT_PATH.'public'.DS.'uploads'.DS.'wdsxh'.DS.'posters'.DS:$savepath;
            if(!is_dir($dirPath)){
                mkdir($dirPath,0777,true);
            }
            $filename =empty($filename)?time().mt_rand(1000,9999).".png":$filename;
            $mainLayer->save($dirPath, $filename, true, null, 95);
            $data=array(
                'file_path'=>$dirPath.$filename,
                'file_name'=>$filename,
                'path'=>'/uploads/wdsxh/posters/'.$filename,
                'url'=>$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/uploads/wdsxh/posters/'.$filename
            );
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        return $data;
    }
}